package com.saver.modules.sys.controller;

import com.saver.common.utils.R;
import com.saver.config.ServerConfig;
import com.saver.modules.sys.dao.SysNoticeDao;
import com.saver.modules.sys.entity.SysNoticeEntity;
import com.saver.modules.sys.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/sys/notice")
public class SysNoticeController extends AbstractController {


    @Autowired
    private SysNoticeDao sysNoticeDao;

    @Autowired
    private SysNoticeService sysNoticeService;

    @Autowired
    private ServerConfig serverConfig;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        return sysNoticeService.queryPage(params);
    }

    @GetMapping("/show")
    public R show() {
        return sysNoticeService.show();
    }

    /**
     * 通知
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        Optional<SysNoticeEntity> byId = sysNoticeDao.findById(id);
        if (byId.isPresent()) {
            return R.ok().put("data", byId.get());
        }
        return R.error("通知不存在");
    }

    @PostMapping("/save")
    public R save(@RequestBody SysNoticeEntity sysNoticeEntity) {
        if (sysNoticeEntity.getId() == null) {
            sysNoticeEntity.setCreateDate(new Date());
            sysNoticeEntity.setCreateUserId(getUserId());
        }
        sysNoticeDao.save(sysNoticeEntity);
        return R.ok();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        return sysNoticeService.deleteBatch(ids);
    }
}
